<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

/**
 * This plugin load all hook targets and regiter all hooks if they are available
 */
class Modules_Core_Controllers_Plugins_HookLoader extends Gio_Core_Plugin 
{
	public function run()
	{
		$targets = Modules_Core_Services_Target::getTargets();
		if ($targets) {
			foreach ($targets as $target) {
				/**
				 * Determine the hook class
				 */
				$hookClass = (null == $target['hook_module'] || '' == $target['hook_module'])
							? 'Hooks_' . ucfirst($target['hook_name']) . '_Hook'
							: 'Modules_' . ucfirst(strtolower($target['hook_module'])) . '_Hooks_' . ucfirst($target['hook_name']) . '_Hook';
				/**
				 * Create new hook instance and register it
				 */
				if (class_exists($hookClass) && (($hook = new $hookClass()) instanceof Gio_Core_Hook)) {
					Gio_Core_Hook_Registry::getInstance()->register($target['target_name'], array($hook, $target['hook_type']));
				}
			}
		}
	}
}
